<?php

namespace Home\Model;
use Think\Model;

class ServiceApisImageModel extends Model {
	
	//获取单条api记录
	public function getInfo($api_id){
		if(empty($api_id)) return null;

		$key = md5('service_api_image_apiid_'.$api_id);

		if(session( md5('37degree_redis') ) === true){//清楚缓存
            S($key,null);
            $info = false;
        }else{
            $info = S($key);
        }
        
		if($info===false){
			$sql = "select api.*,s.service_name_en as services_name_en,s.service_response_heads,s.service_request_heads from dmp_service_apis_image api left join dmp_services s on api.service_id=s.service_id where api.api_id=".(int) $api_id;
			$result = $this->query($sql);

			if(empty($result)){
				$info = array();
			}else{
				$info = $result[0];
			}

			S($key,$info,600);//缓存十分钟
		}
		
		// $info = $this->where('api_id='.(int) $api_id)->find();
		return $info;
	}

	public function getIndexQuery($sk,$size,$offset){
		$sql = "select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction,ser.service_logo,ser.service_publisher,u.auth_type,ifnull(least(min(api.min_price),if(api.api_is_charge=0,0,99999999) ),0) price from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id left join dmp_users u on u.id=ser.uid where ser.service_name like '%".trim($sk) ."%'  and api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en order by ser.service_name_en limit ".$offset.",".$size;
		$list = $this->query($sql);
		return $list;
	}
	
	//根据es中服务编号id，反查询服务相关服务及api信息
	public function getServiceWithEsid($service_ids=array(),$service_ids_bak=array()){
		if(empty($service_ids)) return false;

		$sql = "select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction,ser.service_logo,ser.service_publisher,ser.call_num,u.auth_type,ifnull(least(min(api.min_price),if(api.api_is_charge=0,0,99999999)),0) price,group_concat(DISTINCT tag.name) tag_string from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id left join dmp_service_tag ser_tag on ser_tag.service_id=ser.service_id left join dmp_tag tag on tag.id=ser_tag.tag_id left join dmp_users u on u.id=ser.uid where ser.service_id in (".implode(",",$service_ids) .")  and api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en,ser.service_id order by ser.service_name_en ";
		$list = $this->query($sql);
		// if(!empty($service_ids_bak) && count($list)<count($service_ids)){
		// 	$ids = array_slice($service_ids_bak,0,count($service_ids)-count($list),true);
		// 	$bak_sql = "select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction,ser.service_logo,ser.service_publisher,u.auth_type,ifnull(least(min(api.min_price),min(api.api_is_charge)),0) price from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id left join dmp_users u on u.id=ser.uid where ser.service_id in (".implode(",",$ids) .")  and api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en order by ser.service_name_en ";
		// }

		return $list;
	}

	public function countIndexQuery($sk){
		$sql = "select count(*) num from (select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id where ser.service_name like '%". trim($sk) ."%' and api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en) t";
		$list = $this->query($sql);
		return $list[0];
	}
	
	public function getInfoWithAuth($api_id,$uid){
		if(empty($api_id) || empty($uid)) return null;


	}

	//
	public function getServiceWithApi(){

	}

	//获取所有服务信息
	public function getAllService($page = 1,$pagesize = 20){
		$offset = ( (int)$page-1 )* $pagesize;

		$sql = "select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction,ser.service_logo,ser.service_publisher,u.auth_type from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id left join dmp_users u on u.id=ser.uid where api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en limit ".$offset.",".$pagesize;
		

		$count_sql = "select count(*) num from (select ser.service_id,ser.service_name,ser.service_name_en,ser.service_introduction from dmp_service_apis_image api join dmp_services ser on api.service_id=ser.service_id where api.api_status=1 and api.api_auth_status=2 GROUP BY ser.service_name_en) t";
		$count_info = $this->query($count_sql);

		$count = $count_info[0]['num'];
		$data = $this->query($sql);

		$list['totalpage'] = $count;
		$list['list'] = $data; 
		return $list;

	}

	//查找服务下在线api个数
	public function countOnlineapiWithService($service_id){
		$sql = "select count(service_id) count from dmp_service_apis_image where service_id=".(int)$service_id." and api_status=1 and api_display=1 and api_enable=1 ";
		
		$result = $this->query($sql);
		if(!empty($result)){
			return $result[0]['count'];
		}

		return false;
	}

	//获取服务商名称或服务商公司名称
	public function getProviderInfo($uid){
		if(empty($uid)) return false;

		$sql = "select u.id,u.enterprise_id,u.user_email,u.user_mobile,u.user_name,u.user_type,u.auth_type,e.enterprise_name from dmp_users u left join dmp_enterprises e on u.enterprise_id=e.id where u.id=".(int)$uid;

		$result = $this->query($sql);
		if(!empty($result)){
			return $result[0];
		}
		return null;
	}
	
}

?>